
#include "common_macros.h"

#include "mem.h"


// =============================================================================
inline void mem_copy_function(char * dest, char * src, int nbytes) {
	while (nbytes) {
		*dest = *src;
		dest++;
		src++;
		nbytes--;
	}
}
inline void mem_clear_function(char * dest, int nbytes) {
	while (nbytes) {
		*dest = 0;
		dest++;
		nbytes--;
	}
}

// =============================================================================
boolean mem_copyc(int n, char * x, int incx, char * y, int incy) {

	if (!n) return FALSE;
	if (!x || !y) return FALSE;

	if (incx == 1 && incy == 1) {

		while (n > 0) {
			*x = *y;
			x++;
			y++;
			n--;
		}
		return TRUE;
	}

	while (n > 0) {
		*x = *y;
		x += incx;
		y += incy;
		n--;
	}

	return TRUE;

}
boolean mem_copys(int n, short * x, int incx, short * y, int incy) {

	if (!n) return FALSE;
	if (!x || !y) return FALSE;

	if (incx == 1 && incy == 1) {

		while (n > 0) {
			*x = *y;
			x++;
			y++;
			n--;
		}
		return TRUE;
	}

	while (n > 0) {
		*x = *y;
		x += incx;
		y += incy;
		n--;
	}

	return TRUE;

}
boolean mem_copyi(int n, int * x, int incx, int * y, int incy) {

	if (!n) return FALSE;
	if (!x || !y) return FALSE;

	if (incx == 1 && incy == 1) {

		while (n > 0) {
			*x = *y;
			x++;
			y++;
			n--;
		}
		return TRUE;
	}

	while (n > 0) {
		*x = *y;
		x += incx;
		y += incy;
		n--;
	}

	return TRUE;

}
boolean mem_copyf(int n, float * x, int incx, float * y, int incy) {

	if (!n) return FALSE;
	if (!x || !y) return FALSE;

	if (incx == 1 && incy == 1) {

		while (n > 0) {
			*x = *y;
			x++;
			y++;
			n--;
		}
		return TRUE;
	}

	while (n > 0) {
		*x = *y;
		x += incx;
		y += incy;
		n--;
	}

	return TRUE;

}
boolean mem_copyd(int n, real * x, int incx, real * y, int incy) {

	if (!n) return FALSE;
	if (!x || !y) return FALSE;

	if (incx == 1 && incy == 1) {

		while (n > 0) {
			*x = *y;
			x++;
			y++;
			n--;
		}
		return TRUE;
	}

	while (n > 0) {
		*x = *y;
		x += incx;
		y += incy;
		n--;
	}

	return TRUE;

}
boolean mem_copyr(int n, real * dest, int incd, real * src, int incs) {

	if (!n) return FALSE;
	if (!dest || !src) return FALSE;

	if (incd == 1 && incs == 1) {

		while (n > 0) {
			*dest = *src;
			dest++;
			src++;
			n--;
		}
		return TRUE;
	}

	while (n > 0) {
		*dest = *src;
		dest += incd;
		src += incs;
		n--;
	}

	return TRUE;

}

boolean mem_copymr(int m, int n, real * dest, int incd, real * src, int incs) {

	if (!n) return FALSE;
	if (!dest || !src) return FALSE;
	
	int i, j;
	for( i = 0; i < m; i++) {
		for( j = 0; j < n; j++) {
			*dest = *src;
			dest++;
			src++;
		}
		dest+= (incd - n);
		src+= (incs - n);
	}

	return TRUE;

}
/**
 * The general case
 * @param n - number of elements
 * @param bytes - size (in bytes) of each element
 * @param dest - pointer to destination vector
 * @param incd - increment (of number elements) to next value of destination
 * @param src - pointer to source vector
 * @param incs - increment (of number elements) to next value of source
 * @return 
 */
boolean mem_copyx(int n, int bytes, char * dest, int incd, char * src, int incs) {

	int i;
	if (!n) return FALSE;
	if (!dest || !src) return FALSE;

	incd *= bytes;
	incs *= bytes;

	while (n > 0) {

		for (i = 0; i < bytes; i++) {
			dest[i] = src[i];
		}

		dest += incd;
		src += incs;
		n--;
	}

	return TRUE;

}


